﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using System.Collections;
using System.IO;

namespace School.Admission
{
    public class XSchool
    {
        private static SchoolEntities db = new SchoolEntities();

        public static Boolean Add(SD_XSchool xschool)
        {
            
            
                db.AddToSD_XSchool(xschool);
                db.SaveChanges();
                return true;
            
        }

        public static SD_XSchool Single(long id)
        {
            return db.SD_XSchool.Single(x => x.Id == id);
        }



        public static IEnumerable<SD_XSchool> SingleByStudentId(long sid)
        {
            try
            {
                return db.SD_XSchool.Where(x => x.Student_Id == sid);
            }
            catch
            {
                return null;
            }
        }



        public static IEnumerable<SD_XSchool> All(Hashtable items)
        {
            SchoolEntities db = new SchoolEntities();

            var query = from xschool in db.SD_XSchool select xschool;
            foreach (DictionaryEntry item in items)
            {

                if (item.Key == "Student_Id")
                {
                    query = query.Where(c => c.Student_Id  ==(long) item.Value);
                }


                if (item.Key == "SchoolName")
                {
                    query = query.Where(c => c.SchoolName == item.Value);
                }
            }
            return query.ToList();
        }

        public static void Update(SD_XSchool xschool)
        {
            SD_XSchool c = db.SD_XSchool.Single(x => x.Id == xschool.Id);
            c.Id = xschool.Id;
            c.Student_Id = xschool.Student_Id;
            c.SchoolName = xschool.SchoolName;
            c.SatrtDate = xschool.SatrtDate;
            c.EndDate = xschool.EndDate;
            c.Address = xschool.Address;
            c.Certification = xschool.Certification;
            c.Grade = xschool.Grade;
            c.Class = xschool.Class;
            c.FirstLanguage = xschool.FirstLanguage;
            c.SecondLanguage = xschool.SecondLanguage;

            db.SaveChanges();

        }

        public static Boolean Delete(long id)
        {
            try
            {
                db.SD_XSchool.DeleteObject(db.SD_XSchool.Single(x => x.Id == id));
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static Boolean Deletebystudentid(long sid)
        {
            try
            {
               
                db.ExecuteStoreCommand("Delete From SD_XSchool where Student_Id = " + sid);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
